作者:phpyang | 来源:互联网 | 2023-09-04 14:35
篇首语:本文由编程笔记#小编为大家整理,主要介绍了Ruby‘sAdventrue游戏制作笔记(十五)UnityNPC对话相关的知识,希望对你有一定的参考价值。
篇首语:本文由编程笔记#小编为大家整理,主要介绍了Ruby‘s Adventrue游戏制作笔记(十五)UnityNPC对话相关的知识,希望对你有一定的参考价值。
Ruby's Adventrue游戏制作笔记(十五)UnityNPC对话
- 前言
- 一、拖入NPC
- 二、添加碰撞器
- 三、使用玩家发射射线的方式检测是否有NPC
- 四、 给NPC添加对话框
- 五、给NPC添加脚本
- 六、设置提示框
- 七、NPC对话自定义
- 系列链接
前言
本文章是我学习Unity官方项目项目所做笔记,作为学习Unity的游戏笔记,在最后一章会发出源码,如果等不及可以直接看源码,里面也有很多注释相关,话不多说,让Ruby动起来!
游戏引擎:Unity2020.3
一、拖入NPC
![](https://img6.php1.cn/3cdc5/a056/5a0/111e9f026b8e3d15.jpeg)
调整动画速度 (ctrl + 6)
二、添加碰撞器
调整大小
![](https://img6.php1.cn/3cdc5/a056/5a0/3d53b557ff3f7a2d.jpeg)
三、使用玩家发射射线的方式检测是否有NPC
使用玩家发射射线的方式检测是否有NPC,
![](https://img6.php1.cn/3cdc5/a056/5a0/4fd708d85c935f27.jpeg)
设置NPC层级,使射线只会跟NPC层级交互
![](https://img6.php1.cn/3cdc5/a056/5a0/2e881ef5e9881725.jpeg)
四、 给NPC添加对话框
添加UI-Canvas
![](https://img6.php1.cn/3cdc5/a056/5a0/72f894ac3bc4d276.jpeg)
修改 画布为world Space,并修改对话框大小
![](https://img6.php1.cn/3cdc5/a056/5a0/a190f3fa67768d31.jpeg)
![](https://img6.php1.cn/3cdc5/a056/5a0/0d8bc38f6dc5a41b.jpeg)
稍微调整位置
![](https://img6.php1.cn/3cdc5/a056/5a0/5739266b32edea74.jpeg)
添加背景图片
![](https://img6.php1.cn/3cdc5/a056/5a0/5a94ceb4efedd9ea.jpeg)
选择素材
![](https://img6.php1.cn/3cdc5/a056/5a0/d2d52780e9e5cef7.jpeg)
选择锚点,按住Alt键,选择右下角的这个,进行画布填充
![](https://img6.php1.cn/3cdc5/a056/5a0/5326bf4f48b5f4f4.jpeg)
![](https://img6.php1.cn/3cdc5/a056/5a0/b8ad3978f0f406e2.jpeg)
创建文本
![](https://img6.php1.cn/3cdc5/a056/5a0/96d54f8760f16fb2.jpeg)
![](https://img6.php1.cn/3cdc5/a056/5a0/8eb292742a403b31.jpeg)
添加outline使文字更加清晰
![](https://img6.php1.cn/3cdc5/a056/5a0/aa173b71b213b713.jpeg)
五、给NPC添加脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NPCmanager : MonoBehaviour
public GameObject dialogImage;
public float showTime = 4;
public float showTimer;
private void Start()
dialogImage.SetActive(false);
showTimer = -1;
private void Update()
showTimer -= Time.deltaTime;
if(showTimer < 0)
dialogImage.SetActive(false);
public void ShowDialog()
showTimer &#61; showTime;
dialogImage.SetActive(true);
将DialogFrame拖入脚本
![](https://img6.php1.cn/3cdc5/a056/5a0/5f043448c4eb4758.jpeg)
六、设置提示框
![](https://img6.php1.cn/3cdc5/a056/5a0/d102c8ab506173ac.jpeg)
![](https://img6.php1.cn/3cdc5/a056/5a0/d7b6e97833aacf5b.jpeg)
![](https://img6.php1.cn/3cdc5/a056/5a0/6028dc456f2b1688.jpeg)
设置对话时让提示框消失
![](https://img6.php1.cn/3cdc5/a056/5a0/73721cc382d1dcb1.jpeg)
设置提示框在对话框结束后出现
![](https://img6.php1.cn/3cdc5/a056/5a0/78e5d86b91e52a51.jpeg)
将对话框拖入进去
![](https://img6.php1.cn/3cdc5/a056/5a0/fa2764f06b71f87e.jpeg)
七、NPC对话自定义
在NPC脚本中添加
![](https://img6.php1.cn/3cdc5/a056/5a0/a610e054a118edf0.jpeg)
设置文本
![](https://img6.php1.cn/3cdc5/a056/5a0/c4dd9677ea8463a5.jpeg)
将NPC下的TEXT拖入
![](https://img6.php1.cn/3cdc5/a056/5a0/f3abb1d7177e9835.jpeg)
获得同一个tag下的物品数量
设置robot的tag为Robot
![](https://img6.php1.cn/3cdc5/a056/5a0/48062d06b5c24a95.jpeg)
![](https://img6.php1.cn/3cdc5/a056/5a0/cca2354164aec34d.jpeg)
![](https://img6.php1.cn/3cdc5/a056/5a0/4385583229cb1d7b.jpeg)
![](https://img6.php1.cn/3cdc5/a056/5a0/b4896fd772287658.jpeg)
系列链接
Ruby‘s Adventrue游戏制作笔记&#xff08;一&#xff09;Unity创建项目
Ruby‘s Adventrue游戏制作笔记&#xff08;二&#xff09;Unity控制ruby移动
Ruby‘s Adventrue游戏制作笔记&#xff08;三&#xff09;Unity使用tilemap绘制场景
Ruby‘s Adventrue游戏制作笔记&#xff08;四&#xff09;Unity绘制其他元素
Ruby‘s Adventrue游戏制作笔记&#xff08;五&#xff09;Unity解决碰撞抖动和旋转问题
Ruby‘s Adventrue游戏制作笔记&#xff08;六&#xff09;Unity相机跟随玩家移动
Ruby‘s Adventrue游戏制作笔记&#xff08;七&#xff09;Unity采集生命道具
Ruby‘s Adventrue游戏制作笔记&#xff08;八&#xff09;Unity伤害陷阱
Ruby‘s Adventrue游戏制作笔记&#xff08;九&#xff09;Unity添加敌人
Ruby‘s Adventrue游戏制作笔记&#xff08;十&#xff09;Unity添加动画
Ruby‘s Adventrue游戏制作笔记&#xff08;十一&#xff09;Unity角色攻击——发射子弹
Ruby‘s Adventrue游戏制作笔记&#xff08;十二&#xff09;Unity给角色添加简单的特效
Ruby‘s Adventrue游戏制作笔记&#xff08;十三&#xff09;Unity血条UI的显示
Ruby‘s Adventrue游戏制作笔记&#xff08;十四&#xff09;Unity播放游戏音效
Ruby‘s Adventrue游戏制作笔记&#xff08;十五&#xff09;UnityNPC对话
Ruby‘s Adventrue游戏制作笔记&#xff08;十六&#xff09;Unity子弹数量及其UI
Ruby‘s Adventrue游戏制作笔记&#xff08;十七&#xff09;Unity添加游戏胜利条件和失败条件和导出游戏